/*
 * Copyright 2009 BAKUDO Michał Rabiński	 
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package bakudo.utils.scrollers.uiScroll {
	import bakudo.utils.scrollers.uiScroll.events.UIScrollerEvent;

	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.EventDispatcher;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.geom.Rectangle;
	import flash.utils.Timer;

	/**
	 * @author Bakudo Michal Rabinski
	 */
	public class UIScroller extends EventDispatcher {

		//::::::::::::::: public vars ::::::::::::::://
		public var handle : Sprite;

		public var track : Sprite;

		public var content : DisplayObject;

		public var viewMask : Sprite;

		public var smoothing : Boolean;

		//::::::::::::::: private vars ::::::::::::::://
		private var handleRect : Rectangle;
		private var horizontal : Boolean;
		private var timer : Timer = new Timer(10, 0);
		private var _percent : Number;
		
		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
		// PUBLIC GETTERS
		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
		
		/**
		 * zwraca procentowe polozenie suwaka
		 */
		public function get percent() : Number{
			if(!timer.running)countPercent();
			return _percent;
		}

		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
		// PUBLIC METHODS
		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
		
		/**
		 * podlacza elementy do scrollera
		 * @param h - referencja do uchwytu slidera
		 * @param t - referencja do paska po kotorym jezdzi uchwyt
		 * @param c - referencja do kontentu jesli istnieje
		 * @param m - referencja do maski kontentu jesli ona istnieje
		 */
		public function connect(h : Sprite,t : Sprite,c : DisplayObject = null,m : Sprite = null) : void {
			handle = h;
			track = t;
			if(c)content = c;
			if(m)viewMask = m;
			setAll();
		}

		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
		// PRIVATE METHODS
		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
		
		private function setAll() : void {
			if (track.width > track.height) horizontal = true;
			handleRect = new Rectangle();
			handleRect.width = Math.ceil(handle['rect'].width);
			handleRect.height = Math.ceil(handle['rect'].height);
			timer.addEventListener(TimerEvent.TIMER, onTimerTick, false, 0, true);
			handle.addEventListener(MouseEvent.MOUSE_DOWN, onMouseD, false, 0, true);
			handle.stage.addEventListener(MouseEvent.MOUSE_UP, onMouseU, false, 0, true);
			handle.buttonMode = true;
		}

		private function startScroll() : void {
			timer.start();	
		}

		private function stopScroll() : void {
			timer.stop();	
		}

		private function doScroll() : void {
			countPercent();
			var evtName : String = UIScrollerEvent.SCROLL_UPDATE;
			dispatchEvent(new UIScrollerEvent(evtName, false, false));
		}

		private function doHandleGrasp() : void {
			var rect : Rectangle = new Rectangle();
			rect.x = track.x, 
			rect.y = 0;
			rect.width = horizontal ? track.width - handleRect.width : 0; 
			rect.height = horizontal ? 0 : track.height - handleRect.height;
			handle.startDrag(false, rect);
			startScroll();	
		}

		private function doHandleRelease() : void {
			handle.stopDrag();
			stopScroll();	
		}	
		
		private function countPercent():void{
			var maxval : Number; 
			var curval : Number;
			if (horizontal) {
				maxval = track.width - handleRect.width;
				curval = handle.x;
			}else {
				maxval = track.height - handleRect.height;
				curval = handle.y;
			}
			_percent = curval / maxval;
		}

		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
		// PRIVATE LISTENERS
		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
		private function onTimerTick(event : TimerEvent) : void {
			doScroll();
		}

		private function onMouseD(event : MouseEvent) : void {
			doHandleGrasp();	
		}

		private function onMouseU(event : MouseEvent) : void {
			doHandleRelease();	
		}
	}
}
